# 作者: 菅世博
# 2025年02月27日16时13分19秒
# 512326042@qq.com

# search.findall.sub.split
import re

ret = re.search(r"\d+", "阅读次数为 9999")  # search只能找第一个
print(ret.group())


def find_second_match(pattern, text):
    matches = re.finditer(pattern, text)  # matches是一个迭代器
    try:
        next(matches)  # 跳过第一个匹配项
        second_match = next(matches)  # 获取第二个匹配项
        return second_match.group()
    except StopIteration:
        return None


text = "abc123def456ghi789"
pattern = r"\d+"
second_match = find_second_match(pattern, text)
print(second_match)

print('-' * 50)
ret = re.findall(r"\d+", "python = 9999, c = 7890, c++ = 12345")
print(ret)  # 返回的是列表类型

print('-' * 50)


def add(match_result):
    """
    返回值一定是字符串
    :param match_result:  # 平匹配后的match对象
    :return:
    """
    return str(int(match_result.group()) + 1)


ret = re.sub(r'\d+', '998', '阅读量997')
print(ret)  # 返回的是替换后的效果

ret = re.sub(r'\d+', add, '阅读量997')
print(ret)
